﻿using System.Collections.Generic;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using ProjectManager.Masters.ModuleData;
using System.Resources;

namespace ProjectManager.Masters.Forms
{
    public partial class Projects : ApplicationLibrary.DBManager.DocumentForm, iDocument
    {
        private BindableOject<int> bdoProjectType = new BindableOject<int>();
        private BindableOject<int> bdoFatherType = new BindableOject<int>();
        private BindableOject<int> bdoFather = new BindableOject<int>();
        private EnumsManager<Status> StatusManager = new EnumsManager<Status>(Properties.Resources.ResourceManager);
        private EnumsManager<Counters> TypesManager = new EnumsManager<Counters>(Properties.Resources.ResourceManager);

        #region Interface
        public ApplicationLibrary.ExtendedControls.ExTabControl masterTabControl { get { return exTabControl1; } }
        public Counters ProjectType { get { return Counters.Project; } }
        public int ProjectNumb { get { return cntNumber.CounterNumericValue; } }
        public string Description { get { return txtDescription.Text; } }
        #endregion

        public Projects()
            : base("Projects")
        {
            InitializeComponent();
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerProjects("ProjectForm", new RadarProjects(Counters.Project), this);
            dbManager.AddMaster(this, PM_Project.Name, true);
            dbManager.AddSlave("SLAVESTATUS", false);

            cntNumber.AttachCounterType(GlobalInfo.GetCounter(Properties.Resources.C_Projects), GlobalInfo.CurrentDate, null);
            cbbProduct.AttachDataReader<string>(new RRDescriptions(DescriptionType.E_Products), PM_Descriptions.Code, PM_Descriptions.Description);
            cbbVersion.AttachDataReader<string>(new RRDescriptions(DescriptionType.E_Versions), PM_Descriptions.Code, PM_Descriptions.Description);

            dgwBugs.AddReadOnlyColumn(PM_Project.Type, true);
            dgwBugs.AddReadOnlyColumn(PM_Project.Description, true);
            dgwBugs.AddReadOnlyColumn(PM_Project.Code, true);
            dgwBugs.AddReadOnlyColumn(PM_Project.Status, true);

            colBugsStatus.EnumsType = typeof(Status);
            colBugsType.EnumsType = typeof(Counters);
            colBugsStatus.ResourceManager = Properties.Resources.ResourceManager;
            colBugsType.ResourceManager = Properties.Resources.ResourceManager;

            dgwBugs.AutoGenerateColumns = false;
            dgwBugs.DataSource = dbManager.Dataset.Tables["SLAVESTATUS"];

            StatusManager.AttachTo(cbbStatus);
            base.OnAttachData();
        }

        protected override void OnBindData()
        {
            BindControl(cntNumber, PM_Project.Code, NullValue.NotSet, Findable.YES);
            BindControl(ctbDate, PM_Project.Date);
            BindControl(txtDescription, PM_Project.Description);
            BindControl(cbbProduct, PM_Project.Product, "SelectedValue", NullValue.NotSet);
            BindControl(cbbVersion, PM_Project.Version, "SelectedValue", NullValue.NotSet);
            BindControl(cbbStatus, PM_Project.Status, "SelectedValue", NullValue.NotSet);
            BindControl(ctbStarted, PM_Project.StartingDate);
            BindControl(ctbEnded, PM_Project.EndingDate);
            BindControl(rtbNote, PM_Project.Note, "Rtf");

            BindColumn(colBugsType, PM_Project.Type);
            BindColumn(colBugsNumber, PM_Project.Code);
            BindColumn(colBugsDescription, PM_Project.Description);
            BindColumn(colBugsStatus, PM_Project.Status);

            BindObject(bdoProjectType, PM_Project.Type);
            BindObject(bdoFather, PM_Project.Father);
            BindObject(bdoFatherType, PM_Project.FatherType);

            BindControl(dgwBugs);
            BindCounter(cntNumber);
            base.OnBindData();
        }

        protected override bool OnAfterAddNew()
        {
            bdoFatherType.Value = 0;
            bdoFather.Value = 0;
            bdoProjectType.Value = Counters.Project.Int();
            cbbStatus.SelectedValue = Status.ToDo.Long();
            ctbDate.Today = GlobalInfo.CurrentDate;
            return base.OnAfterAddNew();
        }

        protected override void OnDisableControlsForEdit()
        {
            cntNumber.Enabled = false;
            base.OnDisableControlsForEdit();
        }
    }

    #region DbManager dbmanagerProjects

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerProjects : DBManager
    {
        public dbmanagerProjects(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new PM_Project();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(PM_Project.Type, "@p1");
            qB.AddCompare(PM_Project.Code, "@p2");

            return qB.Query;
        }

        protected override string CreateSlaveQuery(string name)
        {
            if (name == "SLAVESTATUS")
            {
                QueryBuilder qb1 = new QueryBuilder();
                QueryBuilder qb = new QueryBuilder();

                qb1.AddSelect();
                qb1.AddFrom(PM_Project.Name);
                qb1.AddCompare(PM_Project.FatherType, "@p3");
                qb1.AddCompare(PM_Project.Father, "@p4");

                qb.AddManualQuery(@"select Y.* from {0} X inner join {1} Y
                                    on X.{2} = Y.{3} and X.{4} = Y.{5}",
                                        PM_Project.Name,
                                        PM_Project.Name,
                                        PM_Project.Type.Name,
                                        PM_Project.FatherType.Name,
                                        PM_Project.Code.Name,
                                        PM_Project.Father.Name);
                qb.AddCompare(string.Format("X.{0}", PM_Project.FatherType.Name), "@p3", "=");
                qb.AddCompare(string.Format("X.{0}", PM_Project.Father.Name), "@p4", "=");

                return string.Concat(qb1.Query, " UNION ", qb.Query);
            }
            else
                return "";
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", PM_Project.Type);
            sParam1.Value = PM_Project.Type.DefaultValue;
            PList.Add(sParam1);

            SqlABParameter sParam2 = new SqlABParameter("@p2", PM_Project.Code);
            sParam2.Value = PM_Project.Code.DefaultValue;
            PList.Add(sParam2);
            return PList;
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();
            if (name == "SLAVESTATUS")
            {
                SqlABParameter p3 = new SqlABParameter("@p3", PM_Project.FatherType);
                p3.Value = 99999;
                PList.Add(p3);
                SqlABParameter p4 = new SqlABParameter("@p4", PM_Project.Father);
                p4.Value = 0;
                PList.Add(p4);
                SqlABParameter p5 = new SqlABParameter("@p5", PM_Project.Type);
                p5.Value = Counters.Bug.Int();
                PList.Add(p5);
            }

            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
            collection.Parameter[1].Value = key[1];
        }
    }

    #endregion DbManager dbmanagerProjects
}